package com.serviceprovider.business.module.taxpayer.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.taxpayer.dao.ITaxPayerDao;
import com.serviceprovider.business.module.taxpayer.model.TaxPayerInfo;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mrduongnv@gmail.com
 *
 */
@Repository("taxPayerDao")
public class TaxPayerDaoImpl extends SimpleJdbcDaoSupport implements ITaxPayerDao {
    private Logger           logger = Logger.getLogger(getClass());
    @Autowired
    @Qualifier("taxPayerJdbcInsert")
    private SimpleJdbcInsert jdbcInsert;

    @Autowired
    public TaxPayerDaoImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public TaxPayerInfo getTaxPayerDetails(Integer taxPayerSSN) {

        // TODO Auto-generated method stub
        String sql = "select * from tax_payer_info where year(Created_date)=year(curdate()) and tp_SSN_ITIN="
                     + taxPayerSSN;

        try {
            return (TaxPayerInfo) getJdbcTemplate().queryForObject(sql, new TaxPayerInfoMapper());
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public TaxPayerInfo saveTaxPayerInfo(TaxPayerInfo t) {
        Map<String, Object> para = new HashMap<String, Object>();

        para.put("tp_SSN_ITIN", t.getTaxPayerSSNITIN());
        para.put("tp_FirstNm", t.getFirstName());
        para.put("tp_LastNm", t.getLastName());
        para.put("tp_MiddleNm", t.getMiddleName());
        para.put("tp_DOB", t.getDOB());
        para.put("tp_Lic_ID_Num", t.getIdentificationNum());
        para.put("tp_Lic_Id_Issued_By", t.getIssuedBy());
        para.put("tp_Lic_Id_Issue_Dt", t.getIssueDt());
        para.put("tp_Lic_Id_Expiry_Dt", t.getExpDt());
        para.put("tp_Apply_ITIN", t.getApplyItin());
        para.put("tps_SSN_ITIN", t.getSpouseSSNITIN());
        para.put("tps_FirstNm", t.getSpouseFirstNm());
        para.put("tps_LastNm", t.getSpouseLastNm());
        para.put("tps_MiddleNm", t.getSpouseMiddleNm());
        para.put("tps_DOB", t.getSpouseDOB());
        para.put("tps_Lic_ID_Num", t.getSpouseIdentificationNum());
        para.put("tps_Lic_Id_Issued_By", t.getSpouseIssuedBy());
        para.put("tps_Lic_Id_Issue_Dt", t.getSpouseIdIssueDt());
        para.put("tps_Lic_Id_Expiry_Dt", t.getSpouseIdExpDt());
        para.put("tps_Apply_ITIN", t.getSpouseApplyITIN());
        para.put("tp_Res_Addr", t.getResAddr());
        para.put("tp_Street_addr", t.getStreetAddr());
        para.put("tp_Zip_Code", t.getZipCode());
        para.put("tp_Home_Ph", t.getHomePhone());
        para.put("tp_Cell_Num", t.getCellNum());
        para.put("tp_Cell_carrier", t.getCellCarrier());
        para.put("tp_Product_Requested", t.getProductRequestd());
        para.put("tp_Routing_Num", t.getRoutingNum());
        para.put("tp_Acct_Num", t.getAcctNum());
        para.put("tp_Agreement", t.getAgreement());
        para.put("tp_Disclose_Agrmnt", t.getDiscloseAggrement());
        para.put("tp_Use_Agrmnt", t.getUseAggrement());
        para.put("tp_Contract_Agreement", t.getContractAggrement());
        para.put("tp_Match_Needed", t.getMatch());
        para.put("tp_Amt_Expcted", t.getAmountExpcted());
        para.put("tp_Amt_Received", t.getAmountReceived());
        para.put("tp_Comments", t.getComments());
        para.put("tp_Famly_Apply_ITIN", t.getFamilyITIN());
        para.put("tp_Child_Supp", t.getChildSupport());
        para.put("tp_Loans", t.getLoans());
        para.put("tp_back_tax_IRS", t.getBack());
        para.put("tp_Misc", t.getMisc());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        para.put("created_date", format.format(new Date()));

        Number id = jdbcInsert.executeAndReturnKey(para);

        t.setId(id.intValue());

        return t;
    }

    private class TaxPayerInfoMapper implements ParameterizedRowMapper<TaxPayerInfo> {
        @Override
        public TaxPayerInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            TaxPayerInfo taxPayer = new TaxPayerInfo();

            taxPayer.setId(rs.getInt("tp_ID_Num"));
            taxPayer.setAcctNum(rs.getString("tp_Acct_Num"));
            taxPayer.setAgreement(rs.getString("tp_Agreement"));
            taxPayer.setAmountExpcted(rs.getDouble("tp_Amt_Expcted"));
            taxPayer.setAmountReceived(rs.getDouble("tp_Amt_Received"));
            taxPayer.setApplyItin(rs.getString("tp_Apply_ITIN"));
            taxPayer.setTaxPayerSSNITIN(rs.getInt("tp_SSN_ITIN"));
            taxPayer.setFirstName(rs.getString("tp_FirstNm"));
            taxPayer.setLastName(rs.getString("tp_LastNm"));
            taxPayer.setMiddleName(rs.getString("tp_MiddleNm"));

            return taxPayer;
        }
    }
}



